<template>
  <section class="todoapp">
    <TodoHeader @add='add'></TodoHeader>
    <TodoMain :list="showList" @del="del"></TodoMain>
    <TodoFooter :list="list" @clear="clear" :type="type" @changeClick="changeClick"></TodoFooter>
  </section>
</template>

<script>
import '../style/base.css'
import '../style/index.css'

import TodoHeader from './components/TodoHeader.vue'
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'
export default {
  data () {
    return {
      list: JSON.parse(localStorage.getItem('todo')) || [],
      // 任务状态
      type: 'all'
    }
  },
  methods: {
    // 添加
    add (val) {
      this.list.push({
        id: Date.now(),
        name: val,
        isDone: false
      })
    },
    // 删除
    del (id) {
      const index = this.list.findIndex(item => item.id === id)
      this.list.splice(index, 1)
    },
    // 清除已完成
    clear (list) {
      this.list = list
    },
    // 修改状态
    changeClick (e) {
      this.type = e
    }
  },
  computed: {
    showList () {
      if (this.type === 'act') {
        return this.list.filter(item => item.isDone === false)
      } else if (this.type === 'com') {
        return this.list.filter(item => item.isDone === true)
      } else {
        return this.list
      }
    }
  },
  watch: {
    list: {
      deep: true,
      handler () {
        localStorage.setItem('todo', JSON.stringify(this.list))
      }
    }
  },
  filters: {},
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
